/*
 * Copyright (c) 2011 Obix Labs Limited
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:
 * 
 * 		Redistribution of source code must retain the above 
 * 		copyright notice, this list of conditions and the 
 * 		following disclaimer.
 *
 * 		Redistribution in binary form must reproduce the 
 * 		above copyright notice, this list of conditions 
 * 		and the following disclaimer in the documentation 
 * 		and/or other materials provided with the distribution.
 * 
 * 	THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF 
 * 	ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS 
 * 	AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
 * 	FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, 
 * 	ARE HEREBY EXCLUDED. OBIX LABS LIMITED ("Obix Labs") AND ITS 
 * 	LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE 
 * 	AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR 
 * 	ITS DERIVATIVES. IN NO EVENT WILL Obix Labs OR ITS LICENSORS BE 
 * 	LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 
 * 	INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE 
 * 	DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF 
 * 	LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS 
 * 	SOFTWARE, EVEN IF Obix Labs HAS BEEN ADVISED OF THE POSSIBILITY OF 
 * 	SUCH DAMAGES.
 */
package com.obixlabs.commons.types;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

/**
 * <p>
 * A simple type is an entity (class, object) which always consists of, at least, a name 
 * and a description. It is provided to simplify the representation and lookup
 * of simple corporate data e.g. status, process or business codes.  
 * </p>
 * <p>
 * Generally speaking, the {@link #getName() name} portion of the type is intended to hold a short and unique 
 * code representing the data item or entity encapsulated by the type. For example, it can 
 * be used to hold a booking type in a hotel reservation process.  The {@link #getDescription() description}
 *  field on the other hand provides a longer and human readable description of the type. In essence, 
 *  it expands the value in the {@link #getName() name} field to one that is easily understood on face 
 *  value alone.
 * </p>
 */
public class ObixSimpleType implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	/**
	 * <p>
	 * The name of the concept/entity encapsulated by the instance.
	 * </p>
	 */
	private String name;

        /**
         * <p>
         * The description of the concept/entity encapsulated by the instance.
         * </p>
         */	
	private String description;
	
	/**
	 * <p>
	 * Default constructor.
	 * </p>
	 */
	public ObixSimpleType(){}
	
	/**
	 * <p>
	 * The name of the entity encapsulated by the type. This field could be 
	 * set to the value of a lookup code for example.
	 * </p>
	 * @return     The name of the type.
	 */
	@XmlAttribute
	public String getName(){return name;}
	
	/**
	 * <p>
	 * Sets the name of the type or the entity encapsulated by it. A common 
	 * type of value for the name would be lookup, process or business codes. 
	 * </p>
	 * @param name The type name.
	 * @see #getName()
	 */
	public void setName(String name){this.name = name;}
	
	/**
	 * <p>
	 * Returns the human readable description of the type.
	 * </p>
	 * 
	 * @return     The description of the type.
	 */
	@XmlElement
	public String getDescription()
	{return description;}
	
	/**
	 * <p>
	 * Sets the human readable description of the type. Generally speaking, this
	 * should be set to a value that provides a human readable explanation of the 
	 * value in the {@link #getName() name} field.
 	 * </p>
 	 * 
	 * @param description  The human readable description of the type.
	 */
	public void setDescription(String description)
	{this.description = description;}

	@Override
	public String toString(){return getName();}
	
	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object candidate)
	{
		if (this == candidate) return true;
		if (candidate == null) return false;
		
		if (!(candidate instanceof ObixSimpleType)) return false;
		
		ObixSimpleType other = (ObixSimpleType) candidate;
		if (description == null)
		{
			if (other.description != null) return false;
		}
		else if (!description.equals(other.description)) return false;
		
		if (name == null)
		{
			if (other.name != null) return false;
		}
		else if (!name.equals(other.name)) return false;
		
		return true;
	}	
}//end class def